#!/bin/sh /etc/rc.common
# Copyright (C) 2023 沉默の金 <cmzj11@gmail.com>
# Copyright (C) 2022 Lean <coolsnowwolf@gmail.com>
# Copyright (C) 2019-2021 Tianling Shen <cnsztl@immortalwrt.org>

START=90
STOP=10

EXTRA_COMMANDS="check_status"
EXTRA_HELP="	check_status Check running status of utils"

restart_utils="true"

inital_conf(){
	config_load "turboacc"
	config_get "hw_wed" "config" "hw_wed" "0"
	config_get "hw_flow" "config" "hw_flow" "0"
	config_get "sw_flow" "config" "sw_flow" "0"
	config_get "sfe_flow" "config" "sfe_flow" "0"
	config_get "bbr_cca" "config" "bbr_cca" "0"
	config_get "fullcone_nat" "config" "fullcone_nat" "0"
	config_get "fullcone6" "config" "fullcone6" "0"

	[ ! -e "/lib/modules/$(uname -r)/nft_flow_offload.ko" ] && { sw_flow="0"; hw_flow="0"; }
	[ ! -e "/lib/modules/$(uname -r)/tcp_bbr.ko" ] && bbr_cca="0"
}

load_sfe() {
	local kernel_version=$(uname -r)

	[ -e "/lib/modules/$kernel_version/shortcut-fe-cm.ko" ] && modprobe shortcut-fe-cm
	[ -e "/lib/modules/$kernel_version/fast-classifier.ko" ] && modprobe fast-classifier
}

unload_sfe() {
	[ -d /sys/module/shortcut_fe_cm ] && rmmod shortcut_fe_cm
	[ -d /sys/module/fast_classifier ] && rmmod fast_classifier
}

load_wed() {
	local kernel_version=$(uname -r)

	grep -Eq 'mediatek' /etc/openwrt_release && \
	    ! grep -Eq 'mt7915e' /etc/modules.conf && \
		[ -e "/lib/modules/$kernel_version/mt7915e.ko" ] && {
			sed -i '$aoptions mt7915e wed_enable=Y' /etc/modules.conf
			rmmod mt7915e && sleep 1 && modprobe mt7915e && wifi up
		}
}

unload_wed() {
	local kernel_version=$(uname -r)

	grep -Eq 'mediatek' /etc/openwrt_release && \
	    grep -Eq 'mt7915e' /etc/modules.conf && \
		[ -e "/lib/modules/$kernel_version/mt7915e.ko" ] && {
			sed -i '/mt7915e/d' /etc/modules.conf
			rmmod mt7915e && sleep 1 && modprobe mt7915e && wifi up
		}
}

start(){
	inital_conf

	uci set firewall.@defaults[0].flow_offloading="${sw_flow}"
	uci set firewall.@defaults[0].flow_offloading_hw="${hw_flow}"
	uci set firewall.@defaults[0].fullcone="${fullcone_nat}"
	for i in $(uci show firewall|grep "^firewall.@zone\[[0-9]*\]\.fullcone6" |sed  "s/=.*//g"); do
      uci set "$i"="${fullcone6}"
    done
	uci commit firewall

	[ "${sw_flow}" -ne "1" ] && [ "${sfe_flow}" -eq "1" ] && {
		load_sfe
	}
	
	[ "${hw_flow}" -eq "1" ] && [ "${hw_wed}" -eq "1" ] && {
		load_wed
	}

	if [ "${bbr_cca}" -eq "1" ];  then
		sysctl -w net.ipv4.tcp_congestion_control="bbr"
	else
		sysctl -w net.ipv4.tcp_congestion_control="cubic"
	fi

	if [ "${restart_utils}" = "true" ]; then
		echo "DNSMASQ change"
		/etc/init.d/dnsmasq restart >"/dev/null" 2>&1
		/etc/init.d/firewall restart >"/dev/null" 2>&1
	fi
}

stop(){
	inital_conf

	uci set firewall.@defaults[0].flow_offloading="${sw_flow}"
	uci set firewall.@defaults[0].flow_offloading_hw="${hw_flow}"
	uci set firewall.@defaults[0].fullcone="${fullcone_nat}"
	for i in $(uci show firewall|grep "^firewall.@zone\[[0-9]*\]\.fullcone6" |sed  "s/=.*//g"); do
      uci set "$i"="${fullcone6}"
    done
	uci commit firewall

	[ "${hw_wed}" -eq "0" ] && {
		unload_wed
	}

	unload_sfe

	if [ "${restart_utils}" = "true" ]; then
		echo "DNSMASQ revert"
		/etc/init.d/dnsmasq restart >"/dev/null" 2>&1
		/etc/init.d/firewall restart >"/dev/null" 2>&1
	fi
}

restart(){
	restart_utils="false"

	stop
	start

	echo "DNSMASQ restart"
	/etc/init.d/dnsmasq restart >"/dev/null" 2>&1
	/etc/init.d/firewall restart >"/dev/null" 2>&1
}

check_status(){
	case "$1" in
	"fastpath")
		if [ "$(cat "/sys/module/nft_flow_offload/refcnt" 2>"/dev/null" || echo -1)" -gt "0" ]; then
			msg="Flow Offloading"
			exit_code=0
		elif [ "$(cat "/sys/module/nft_flow_offload/refcnt" 2>"/dev/null")" -eq "0" ] && \
		    [ -e /sys/kernel/debug/hnat/hnat_version ]; then
			msg="MediaTek HWNAT"
			exit_code=0
		elif [ -d /sys/kernel/debug/ecm/ecm_nss_ipv4 ]; then
			msg="QCA-NSS-ECM"
			exit_code=0
		elif [ -d /sys/kernel/debug/ecm/ecm_sfe_ipv4 ]; then
			msg="QCA-ECM-SFE"
			exit_code=0
		elif [ -d /sys/module/fast_classifier ]; then
			msg="Shortcut-FE"
			exit_code=0
		elif [ -d /sys/module/shortcut_fe_cm ]; then
			msg="Shortcut-FE ECM"
			exit_code=0
		else
			msg=""
			exit_code=1
		fi
		;;
	"fullconenat")
    	if [ "$(cat "/sys/module/nft_fullcone/refcnt" 2>"/dev/null" || echo -1)" -gt "0" ]; then
			exit_code=0
		else
			exit_code=1
		fi
		;;
	"bbr")
		[ "x$(cat "/proc/sys/net/ipv4/tcp_congestion_control" 2>"/dev/null")" = "xbbr" ] && \
			exit 0 || exit 1
		;;
	*)
		exit 2
		;;
	esac
	if [ "$msg" != "" ] && { [ $# = 1 ] || { [ $# = 2 ] && [ "$2" = 1 ]; }; }; then
    	echo -n "$msg"
	fi
	exit "$exit_code"
}
